export class GnObjectUtils {
  static deepCopy(obj: any): any {
    const json = JSON.stringify(obj);
    return JSON.parse(json);
  }

  static copy(source: any, dest: any): void {
    for (const key of source) {
      dest[key] = source[key];
    }
  }

  static getValue(obj: any, key: string): any {
    let value = obj;
    key.split('.').forEach(k => value ? value = value[k] : null);
    return value;
  }

  static callFunc(obj: any, method: string, ...args: any[]): any {
    const func = GnObjectUtils.getValue(obj, method);
    const keys = method.split('.');
    keys.pop();
    const funcTargetKey = keys.join('.');
    const funcTarget = funcTargetKey === '' ? obj : GnObjectUtils.getValue(obj, funcTargetKey);
    return func.call(funcTarget, args);
  }
}
